Creating a data set

library(tibble)
data <- tibble(
  Country = c("Japan", "South Korea", "Taiwan", "New Zealand", "China", "Bangladesh", "India", "Egypt", "Iraq", "Algeria", "Iran", "Libya", "Gabon", "Mauritius", "Togo", "Rwanda", "Ethiopia", "Liberia", "Venezuela", "Nicaragua", "United States", "UK", "Norway", "Northern Cyprus", "Montenegro", "France", "Switzerland", "Russia", "Ukraine", "Belarus", "Kyrgyzstan", "Uzbekistan", "Turkmenistan"),
  Region = c("Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Middle East and North Africa", "Middle East and North Africa", "Middle East and North Africa", "Middle East and North Africa", "Middle East and North Africa", "Sub-Saharan Africa", "Sub-Saharan Africa", "Sub-Saharan Africa", "Sub-Saharan Africa", "Sub-Saharan Africa", "Sub-Saharan Africa", "Americas", "Americas", "Americas", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Former Soviet Union", "Former Soviet Union", "Former Soviet Union", "Former Soviet Union", "Former Soviet Union", "Former Soviet Union"),
  PercentageBelieveVaccinesSafe = c(30, 40, 50, 60, 70, 80, 90, 100, 30, 40, 50, 60, 70, 80, 90, 100, 30, 40, 50, 60, 70, 80, 90, 100, 30, 40, 50, 60, 70, 80, 90, 100, 30)
)



write.csv(data, "vaccine_belief_data.csv", row.names = FALSE)

print(data)
## # A tibble: 33 × 3
##    Country     Region                       PercentageBelieveVaccinesSafe
##    <chr>       <chr>                                                <dbl>
##  1 Japan       Asia                                                    30
##  2 South Korea Asia                                                    40
##  3 Taiwan      Asia                                                    50
##  4 New Zealand Asia                                                    60
##  5 China       Asia                                                    70
##  6 Bangladesh  Asia                                                    80
##  7 India       Asia                                                    90
##  8 Egypt       Middle East and North Africa                           100
##  9 Iraq        Middle East and North Africa                            30
## 10 Algeria     Middle East and North Africa                            40
## # ℹ 23 more rows

First Redesign Code

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)


vaccine_data <- read_csv("vaccine_belief_data.csv")
## Rows: 33 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Country, Region
## dbl (1): PercentageBelieveVaccinesSafe
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
unique_regions <- unique(vaccine_data$Region)
all_regions <- c('All', unique_regions)

dropdown_buttons <- lapply(all_regions, function(region) {
  if (region == 'All') {
    return(list(method = "restyle",
                args = list('visible', rep(TRUE, length(unique_regions))),
                label = region))
  } else {
    return(list(method = "restyle",
                args = list('visible', 
                            lapply(unique_regions, function(x) x == region)),
                label = region))
  }
})


plot_data <- lapply(unique_regions, function(region) {
  subset(vaccine_data, Region == region)
})


p <- plot_ly()

for(i in seq_along(plot_data)) {
  p <- add_trace(p, data = plot_data[[i]], x = ~Country, y = ~PercentageBelieveVaccinesSafe,
                 type = 'bar', name = unique_regions[i], visible = TRUE)
}


final_plot <- p %>% layout(title = "Percentage of People Who Believe Vaccines Are Safe by Country and Global Region",
                           xaxis = list(title = "Country"),
                           yaxis = list(title = "Percentage Who Believe Vaccines Are Safe"),
                           updatemenus = list(
                             list(
                               active = -1,
                               buttons = dropdown_buttons
                             )
                           ))


final_plot

Second Redesign Code

library(shiny)
library(plotly)
library(readr)
library(dplyr)

ui <- fluidPage(
  titlePanel("Vaccine Safety Belief by Country and Region"),
  sidebarLayout(
    sidebarPanel(
      numericInput("percentage", "Percentage threshold:", min = 0, max = 100, value = 50),
      textInput("country", "Country:", ""),
      actionButton("update", "Update View")
    ),
    mainPanel(plotlyOutput("plot"))
  )
)

server <- function(input, output) {
  

  filtered_data <- reactive({
    req(input$update)  # This makes sure the code waits for the update button to be pressed
    data <- read_csv("vaccine_belief_data.csv")
    data <- data %>% 
      filter(PercentageBelieveVaccinesSafe <= input$percentage,
             grepl(input$country, Country, ignore.case = TRUE))
  })
  
  output$plot <- renderPlotly({
    fig <- plot_ly(filtered_data(), x = ~PercentageBelieveVaccinesSafe, y = ~Country, type = 'scatter', mode = 'markers+text',
                   text = ~paste(Country, ':', PercentageBelieveVaccinesSafe, '%'), textposition = 'right',
                   marker = list(size = 10), color = ~Region, hoverinfo = 'text') %>%
      layout(title = 'Vaccine Safety Belief by Country and Region',
             xaxis = list(title = 'Percentage Believing Vaccines Are Safe'),
             yaxis = list(title = ''))
    fig
  })
  

  observeEvent(input$update, {
    filtered_data()
  })
}


shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents